<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import logger from '@/utils/logger'
import { fetchGoodCommentsList, replyGoodComments } from '@/api/goodCommentsApi'
import dayjs from 'dayjs'

// 表格数据和加载状态
const tableData = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 查询条件
const queryFields = ref({
  queryFields: {
    evaluationScore: '',
  },
})

// 回复弹窗
const replyDialogVisible = ref(false)
const replyForm = ref({
  evaluateGoodsId: '',
  replyEvaluate: '',
})
const currentRow = ref(null)

const openReplyDialog = (row) => {
  currentRow.value = row
  replyForm.value.evaluateGoodsId = row.evaluateGoodsId
  replyDialogVisible.value = true
}

const closeReplyDialog = () => {
  replyDialogVisible.value = false
  currentRow.value = null
  replyForm.value = {
    evaluateGoodsId: '',
    replyEvaluate: '',
  }
}

const handleReply = async () => {
  try {
    const params = {
      ...currentRow.value,
      replyEvaluate: replyForm.value.replyEvaluate,
      editType: 1, // 供应商编辑
    }
    const res = await replyGoodComments(params)
    if (res.success) {
      ElMessage.success('回复成功')
      closeReplyDialog()
      fetchData()
    } else {
      ElMessage.error('回复失败')
    }
  } catch (error) {
    logger.error('回复失败', error)
    ElMessage.error('回复失败')
  }
}

// 获取数据
const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      queryFields: queryFields.value.queryFields,
      currentPage: currentPage.value,
      pageSize: pageSize.value,
    }
    const res = await fetchGoodCommentsList(params)

    if (res.success) {
      tableData.value = res.data.list || []
      total.value = res.data.total || 0
    } else {
      ElMessage.error('获取评论列表失败')
    }
  } catch (error) {
    logger.error('获取评论列表失败', error)
    ElMessage.error('获取评论列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

// 重置搜索
const resetSearch = () => {
  queryFields.value = {
    queryFields: {
      evaluationScore: '',
    },
  }
  currentPage.value = 1
  fetchData()
}

// 分页处理
const handleSizeChange = (size) => {
  pageSize.value = size
  fetchData()
}

const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<template>
  <div class="main-container">
    <el-dialog
      v-model="replyDialogVisible"
      title="回复评论"
      width="500px"
      append-to-body
      destroy-on-close
      @close="closeReplyDialog"
    >
      <el-form :model="replyForm" label-width="80px">
        <el-form-item label="回复内容">
          <el-input
            v-model="replyForm.replyEvaluate"
            type="textarea"
            :rows="4"
            placeholder="请输入回复内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="closeReplyDialog">取 消</el-button>
        <el-button type="primary" @click="handleReply">确 定</el-button>
      </template>
    </el-dialog>

    <div class="controller-container">
      <el-form :inline="true" :model="queryFields">
        <el-form-item label="评分">
          <el-select
            v-model="queryFields.queryFields.evaluationScore"
            placeholder="请选择评分"
            clearable
            style="width: 150px"
          >
            <el-option
              v-for="score in [0, 1, 2, 3, 4, 5]"
              :key="score"
              :label="score"
              :value="score"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <template #icon>
              <el-icon><LineMdSearch /></el-icon>
            </template>
            搜索
          </el-button>
          <el-button @click="resetSearch">
            <template #icon>
              <el-icon><LineMdClose /></el-icon>
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-container">
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <!-- 商品图片 -->
        <el-table-column label="商品图片" width="100" align="center">
          <template #default="{ row }">
            <el-image
              v-if="row.goodsPicture"
              :src="row.goodsPicture"
              style="width: 60px; height: 60px; border-radius: 4px"
              :preview-src-list="[row.goodsPicture]"
              fit="cover"
              preview-teleported
            />
            <div v-else class="no-image">无图片</div>
          </template>
        </el-table-column>
        <!-- 商品名称 -->
        <el-table-column
          prop="goodsName"
          label="商品名称"
          show-overflow-tooltip
          min-width="120"
          align="center"
        />
        <!-- 规格 -->
        <el-table-column prop="specifications" label="规格" min-width="80" align="center" />
        <!-- 单位 -->
        <el-table-column prop="unit" label="单位" min-width="60" align="center" />
        <!-- 商品类型 -->
        <el-table-column
          prop="dealerGoodsTypeName"
          label="商品类型"
          min-width="100"
          align="center"
        />

        <!-- 用户评论图片 -->
        <el-table-column label="评论图片" width="120" align="center">
          <template #default="{ row }">
            <template v-if="row.imgPathsList && row.imgPathsList.length > 0">
              <el-image
                v-for="(img, idx) in row.imgPathsList"
                :key="idx"
                :src="img"
                style="width: 40px; height: 40px; border-radius: 4px; margin-right: 4px"
                :preview-src-list="row.imgPathsList"
                fit="cover"
                preview-teleported
              />
            </template>
            <template v-else>
              <div class="no-image">无图片</div>
            </template>
          </template>
        </el-table-column>
        <!-- 用户评论内容 -->
        <el-table-column
          prop="evaluateContent"
          label="评论内容"
          min-width="200"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column prop="evaluationScore" label="评分" width="180" align="center">
          <template #default="{ row }">
            <el-rate v-model="row.evaluationScore" disabled />
          </template>
        </el-table-column>
        <el-table-column prop="evaluateTime" label="评论时间" width="180" align="center">
          <template #default="{ row }">
            {{ dayjs(row.evaluateTime).format('YYYY-MM-DD HH:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="replyEvaluate"
          label="回复内容"
          min-width="200"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column prop="replyTime" label="回复时间" width="180" align="center">
          <template #default="{ row }">
            <span v-if="row.replyTime">{{
              dayjs(row.replyTime).format('YYYY-MM-DD HH:mm:ss')
            }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="openReplyDialog(row)">
              <template #icon>
                <el-icon><LineMdChatRoundDots /></el-icon>
              </template>
              回复
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.main-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  gap: 10px;
  .controller-container {
    border-radius: 10px;
    background-color: #fff;
    padding: 15px;
    .el-form {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 10px;
    }
  }
  .table-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background-color: #fff;
    border-radius: 10px;
    padding: 15px;
    overflow: hidden;
    gap: 10px;
    .el-table {
      flex: 1;
    }
    .no-image {
      width: 60px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #909399;
      background-color: #f5f7fa;
      font-size: 12px;
      border-radius: 4px;
    }
  }
  .pagination-container {
    display: flex;
    justify-content: flex-end;
    align-items: flex-end;
  }
}
</style>
